﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace ZK.Common
{
    public class SetScreenSize
    {
        public SetScreenSize()
        {

        }

        /// <summary>
        /// 以1920*1080为标准，当前分辨率和这个宽度的比率
        /// </summary>
        /// <returns>x轴比率</returns>
        public static float xRate()
        {
            float f = 1;
            int screeWidth = Screen.PrimaryScreen.Bounds.Width;
            f = (float)screeWidth / (float)1920;
            return f;
        }

        /// <summary>
        ///  以1920*1080为标准，当前分辨率和这个高度的比率
        /// </summary>
        /// <returns>y轴比率</returns>
        public static float yRate()
        {
            float f = 1;
            int screeHeight = Screen.PrimaryScreen.Bounds.Height;
            f = (float)screeHeight / (float)1080;
            return f;
        }

        /// <summary>
        /// 以1920*1080为标准，x向比率和y向比率小的为字体的系数
        /// </summary>
        /// <returns>x向比率和y向比率小的</returns>
        public static float sizeRate()
        {
            return xRate() <= yRate() ? xRate() : yRate();
        }

        /// <summary>
        /// 递归设置控件和其子控件，使之支持分辨率自适应
        /// </summary>
        /// <param name="con"></param>
        public static void setControls(Control con)
        {
            if (Screen.PrimaryScreen.Bounds.Width != 1920 || Screen.PrimaryScreen.Bounds.Height != 1080)
            {
                if (con is UserControl)
                {
                    (con as UserControl).AutoScaleMode = AutoScaleMode.None;
                }

                Size Max = new Size(Convert.ToInt32(con.MaximumSize.Width * xRate()), Convert.ToInt32(con.MaximumSize.Height * yRate()));
                //Size Min = new Size(Convert.ToInt32(con.MinimumSize.Width * xRate()), Convert.ToInt32(con.MinimumSize.Height * yRate()));
                con.MaximumSize = Max;
                con.MinimumSize = Max;

                con.Width = Convert.ToInt32(con.Width * xRate());
                con.Height = Convert.ToInt32(con.Height * yRate());

                con.Margin = new Padding(Convert.ToInt32(con.Margin.Left * xRate()), Convert.ToInt32(con.Margin.Top * yRate()), Convert.ToInt32(con.Margin.Right * xRate()), Convert.ToInt32(con.Margin.Bottom * yRate()));
                con.Padding = new Padding(Convert.ToInt32(con.Padding.Left * xRate()), Convert.ToInt32(con.Padding.Top * yRate()), Convert.ToInt32(con.Padding.Right * xRate()), Convert.ToInt32(con.Padding.Bottom * yRate()));

                setLocation(con);

                if (con.Controls.Count <= 0)
                {
                    con.Font = new Font(con.Font.Name, con.Font.Size * sizeRate(), con.Font.Style, con.Font.Unit);
                }

                foreach (Control c in con.Controls)
                {
                    setControls(c);
                }
            }

        }

        /// <summary>
        ///  以1920*1080为标准，不同的屏幕分辨率设置不同的空间坐标
        /// </summary>
        /// <param name="cons">需要设置的控件</param>
        public static void setLocation(Control cons)
        {
            int x = Convert.ToInt32(cons.Location.X * xRate()) < 0 ? 0 : Convert.ToInt32(cons.Location.X * xRate());
            int y = Convert.ToInt32(cons.Location.Y * yRate()) < 0 ? 0 : Convert.ToInt32(cons.Location.Y * yRate());
            cons.Location = new Point(x, y);
        }
    }
}
